import React, {
    createContext,
    useContext,
    useState,
} from "react";
import {
     getStatistics, getStatisticsEvents, getTimeSlot,
} from "../services/index";
import {ITimeSlot} from "../types/calculate";
import {IStatistics, IStatisticsMoods} from "../types/statistics";
import {showToast} from "../utils/toast";

interface StatisticsContextData {
    setSelectTypeIndex: (index) => {},
    eventsName: '',
    statisticsTimeSlot: ITimeSlot[];
    statistics: IStatistics[];
    statisticsMoods: IStatisticsMoods[];
    setStatistics: ITimeSlot[];
    statisticsEvents: [];
    loadStatistics: () => Promise<void>;
    loadCalculateChildren: (params: { endDate: string; type: string; events: any[]; startDate: string }) => Promise<void>;
    loadStatisticsTimeSlot: () => Promise<void>;
}

const StatisticsContext = createContext({} as StatisticsContextData);

interface CalculateProviderProps {
    children: React.ReactNode;
}

export const StatisticsProvider: React.FC<CalculateProviderProps> = ({ children }) => {

    const [ selectIndex, setSelectIndex ] = useState(1);
    const [statistics, setStatistics] = useState([]);
    const [statisticsMoods, setStatisticsMoods] = useState([]);
    const [statisticsEvents, setStatisticsEvents] = useState([]);
    const [eventsName, setEventsName] = useState('');
    const [statisticsTimeSlot, setStatisticsTimeSlot] = useState([]);

    async function loadStatistics(params) {
        const res = await getStatistics(params);
        console.log('--loadStatistics-res-',res)
        if (res?.code === 0) {
            setStatistics(res.data?.events||[]);
            setStatisticsMoods(res.data?.moods||[]);
            setEventsName(res.data?.eventName);
        } else {
            showToast("error", "出错了", res?.msg);
        }
    }

    async function loadStatisticsTimeSlot(params) {
        const res = await getTimeSlot(params);
        console.log('--loadStatisticsTimeSlot-res-',res)
        if (res?.code === 0) {
            setStatisticsTimeSlot(res.data?.dates||[]);
        }else {
            showToast("error", "出错了", res?.msg);
        }
    }

    async function loadStatisticsEvents(params) {
        const res = await getStatisticsEvents(params);
        if (res?.code === 0) {
            console.log('--loadStatisticsEvents-res-',res)
            setStatisticsEvents(res.data?.events||[]);
        }else {
            showToast("error", "出错了", res?.msg);
        }
    }

    async function setSelectTypeIndex(index) {
        setSelectIndex(index);
    }

    return (
        <StatisticsContext.Provider
            value={{
                setSelectTypeIndex,
                selectIndex,
                eventsName,
                statisticsTimeSlot,
                statistics,
                statisticsMoods,
                statisticsEvents,
                setStatistics,
                loadStatistics,
                loadStatisticsTimeSlot,
                loadStatisticsEvents,
            }}
        >
            {children}
        </StatisticsContext.Provider>
    );
};

export function useStatistics() {
    const context = useContext(StatisticsContext);

    if (!context) throw new Error("useCalculate must be used inside a CalculateProvider");

    return context;
}
